package cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinsku;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinsku.vo.WarehouseInSkuPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinsku.vo.WarehouseInSkuRespVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinsku.vo.WarehouseInSkuSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinsku.WarehouseInSkuDO;
import cn.iocoder.yudao.module.system.service.erp.warehouseinsku.WarehouseInSkuService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 入库单sku")
@RestController
@RequestMapping("/erp/warehouse-in-sku")
@Validated
public class WarehouseInSkuController {

    @Resource
    private WarehouseInSkuService warehouseInSkuService;

    @PostMapping("/create")
    @Operation(summary = "创建入库单sku")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-sku:create')")
    public CommonResult<Long> createWarehouseInSku(@Valid @RequestBody WarehouseInSkuSaveReqVO createReqVO) {
        return success(warehouseInSkuService.createWarehouseInSku(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新入库单sku")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-sku:update')")
    public CommonResult<Boolean> updateWarehouseInSku(@Valid @RequestBody WarehouseInSkuSaveReqVO updateReqVO) {
        warehouseInSkuService.updateWarehouseInSku(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除入库单sku")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-sku:delete')")
    public CommonResult<Boolean> deleteWarehouseInSku(@RequestParam("id") Long id) {
        warehouseInSkuService.deleteWarehouseInSku(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入库单sku")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-sku:query')")
    public CommonResult<WarehouseInSkuRespVO> getWarehouseInSku(@RequestParam("id") Long id) {
        WarehouseInSkuDO warehouseInSku = warehouseInSkuService.getWarehouseInSku(id);
        return success(BeanUtils.toBean(warehouseInSku, WarehouseInSkuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入库单sku分页")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-sku:query')")
    public CommonResult<PageResult<WarehouseInSkuRespVO>> getWarehouseInSkuPage(@Valid WarehouseInSkuPageReqVO pageReqVO) {
        PageResult<WarehouseInSkuDO> pageResult = warehouseInSkuService.getWarehouseInSkuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseInSkuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入库单sku Excel")
    @PreAuthorize("@ss.hasPermission('erp:warehouse-in-sku:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarehouseInSkuExcel(@Valid WarehouseInSkuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseInSkuDO> list = warehouseInSkuService.getWarehouseInSkuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入库单sku.xls", "数据", WarehouseInSkuRespVO.class,
                        BeanUtils.toBean(list, WarehouseInSkuRespVO.class));
    }

}